<template>
  <page-header-wrapper>
    <a-card :bordered="false">
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="4" :sm="24">
              <a-form-item label="设备编号">
                <a-input v-model="queryParam.equipmentNo" placeholder=""/>
              </a-form-item>
            </a-col>
            <a-col :md="4" :sm="24">
              <a-form-item label="开始时间">
                <a-DatePicker onChange="{onChange}" />
              </a-form-item>
            </a-col>
            <a-col :md="4" :sm="24">
              <a-form-item label="结束时间">
                <a-DatePicker onChange="{onChange}" />
              </a-form-item>
            </a-col>
            <a-col :md="!advanced && 8 || 24" :sm="24">
              <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
              <a-button style="margin-left: 8px" @click="() => this.queryParam = {}">导出</a-button>
            </a-col>
          </a-row>
        </a-form>
      </div>

      <s-table
        ref="table"
        size="default"
        rowKey="key"
        :columns="columns"
        :data="loadData"
        :alert="true"
        :rowSelection="rowSelection"
        showPagination="auto"
      >
        <span slot="serial" slot-scope="text, record, index">
          {{ index + 1 }}
        </span>
        <span slot="equipmentNo" slot-scope="text">
          <ellipsis :length="4" tooltip>{{ text }}</ellipsis>
        </span>
        <span slot="alarmsContentDetail" slot-scope="text">
          <ellipsis :length="10" tooltip>{{ text }}</ellipsis>
        </span>
      </s-table>

    </a-card>
  </page-header-wrapper>
</template>

<script>
import moment from 'moment'
import { STable, Ellipsis } from '@/components'
import { getHistoryList } from '@/api/device'

import StepByStepModal from './modules/StepByStepModal'

const columns = [
  {
    title: 'ID',
    scopedSlots: { customRender: 'serial' }
  },
  {
    title: '设备编号',
    dataIndex: 'equipmentNo'
  },

  {
    title: '设备数据',
    dataIndex: 'alarmsContentDetail'
  },
  {
    title: '采集时间',
    dataIndex: 'updateTime',
    sorter: true
  },
]


export default {
  name: 'TableList',
  components: {
    STable,
    Ellipsis,
    StepByStepModal
  },
  data () {
    this.columns = columns
    return {
      // create model
      visible: false,
      confirmLoading: false,
      mdl: null,
      // 高级搜索 展开/关闭
      advanced: false,
      // 查询参数
      queryParam: {},
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        const requestParameters = Object.assign({}, parameter, this.queryParam)
        console.log('loadData request parameters:', requestParameters)
        return getHistoryList(requestParameters)
          .then(res => {
            const data = res.data
            data.list.forEach(element => {
              element.updateTime = moment(element.updateTime).format('YYYY-MM-DD HH:mm:ss')
            })
            return data
          })
      },
      selectedRowKeys: [],
      selectedRows: [],
      groupList: []
    }
  },
  filters: {
    statusFilter(type) {
      return statusMap[type].text
    }
  },
  created() {
    this.getGroupList()
  },
  computed: {
    rowSelection() {
      return {
        selectedRowKeys: this.selectedRowKeys,
        onChange: this.onSelectChange
      }
    }
  },
  methods: {
    getGroupList() {
      var params = {
        pageNum: 1,
        pageSize: 100
      }
      getDeviceGroupList(params).then(res => {
        this.groupList = res.data.list
        console.log('groupList', this.groupList)
      })
    },
    handleDeleteOk(id) {
      var that = this
      this.$confirm({
        title: '确认要删除吗',
        onOk() {
          deleteDevice(id).then(res => {
            if (res.code == 200) {
              // 刷新表格
              that.$refs.table.refresh(true)
              that.$message.info('删除成功')
            } else {
              that.$message.info(res.message)
            }
          })
        },
        onCancel() {
        }
      })
    },
    handleAdd() {
      this.mdl = null
      this.visible = true
    },
    handleEdit(record) {
      this.visible = true
      console.log('1', record)
      this.mdl = { ...record }
    },
    handleOk() {
      const form = this.$refs.createModal.form
      this.confirmLoading = true
      form.validateFields((errors, values) => {
        if (!errors) {
          console.log('values', values)
          if (values.id > 0) {
            updateDevice(values).then(res => {
              console.log('res', res)
              this.visible = false
              this.confirmLoading = false
              // 重置表单数据
              form.resetFields()
              // 刷新表格
              this.$refs.table.refresh()
              if (res.code == 200) {
                this.$message.info('修改成功')
              } else {
                this.$message.info(res.message)
              }
            })
          } else {
            // 新增
            createDevice(values).then(res => {
              console.log('res', res)
              this.visible = false
              this.confirmLoading = false
              // 重置表单数据
              form.resetFields()
              // 刷新表格
              this.$refs.table.refresh()
              if (res.code == 200) {
                this.$message.info('新增成功')
              } else {
                this.$message.info(res.message)
              }
            })
          }
        } else {
          this.confirmLoading = false
        }
      })
    },
    handleCancel() {
      this.visible = false

      const form = this.$refs.createModal.form
      form.resetFields() // 清理表单数据（可不做）
    },
    handleSub(record) {
      if (record.status !== 0) {
        this.$message.info(`${record.no} 订阅成功`)
      } else {
        this.$message.error(`${record.no} 订阅失败，规则已关闭`)
      }
    },
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    toggleAdvanced() {
      this.advanced = !this.advanced
    },
    resetSearchForm() {
      this.queryParam = {
        date: moment(new Date())
      }
    }
  }
}
</script>
